package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.ExamMenuDao;
import com.woniuxy.entity.ExamMenu;
import com.woniuxy.entity.ExamPublishexam;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.ExamMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ExamMenuServiceImpl implements ExamMenuService {
    @Autowired(required = false)
    ExamMenuDao examMenuDao;

    @Autowired(required = false)
    ExamMenu examMenu;
    @Override
    public ResponseEntity findAll() {
        List<ExamMenu> list = examMenuDao.findAll();
        System.out.println(list);
        return new ResponseEntity<>(200,"success", list);
    }

    @Override
    public ResponseEntity findAll(int currentPage, int pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<ExamMenu> all = examMenuDao.findAll();
        PageInfo<ExamMenu> pageInfo = PageInfo.of(all);
        return new ResponseEntity<>(200,"查询成功",pageInfo);
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ExamMenu examMenu = new ExamMenu();
        examMenu.setUpdate_time(sf.format(date));

        examMenu.setId(id);
//        return new ResponseEntity(200,"删除成功",null);
        System.out.println(examMenu.getUpdate_time());
        return new ResponseEntity(200,"删除成功",examMenuDao.deleteOne(examMenu));
//        int i = examMenuDao.deleteOne(examMenu);
//        if (i>0){
//            return new ResponseEntity(200,"删除成功",null);
//        }else {
//            return new ResponseEntity(500,"删除失败",null);
//        }
    }

    @Override
    public ResponseEntity updateOne(ExamMenu examMenu) {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        examMenu.setUpdate_time(sf.format(date));
        String name = examMenu.getName();
        ExamMenu obj = examMenuDao.findByName(name);
        if (obj != null){
            return new ResponseEntity(200,"修改失败,已经有同名题库请修改名称",null);
        }else {
            return new ResponseEntity(200,"修改成功",examMenuDao.updateOne(examMenu));
        }
    }

    @Override
    public ResponseEntity addOne(ExamMenu examMenu) {
        String name = examMenu.getName();
        System.out.println("年级" + name);
        ExamMenu obj = examMenuDao.findByNameAndFid(examMenu);
        Date date = new Date();
        System.out.println(obj);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        examMenu.setCreate_time(sf.format(date));
        System.out.println("chuangjianshijian" + examMenu.getCreate_time());
        if (obj != null){
            return new ResponseEntity(500,"新增失败,不能增加相同名字的题库",null);
        }else {
            return new ResponseEntity(200, "新增成功", examMenuDao.addOne(examMenu));
        }
    }

    @Override
    public ResponseEntity addOneNoFid(ExamMenu examMenu) {
        String name = examMenu.getName();
        ExamMenu obj = examMenuDao.findByName(name);
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        examMenu.setCreate_time(sf.format(date));
        System.out.println("创建时间:" + examMenu.getCreate_time());
        System.out.println(examMenu.getName());
        if (obj != null){
            return new ResponseEntity(500,"新增失败,不能增加相同名字的题库",null);
        }else {
            return new ResponseEntity(200, "新增成功", examMenuDao.addOneNoFid(examMenu));
        }
    }

    @Override
    public ResponseEntity findBySearch(String name,int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        System.out.println(currentPage + "--" + pageSize);
        System.out.println("impl" + name);
        String replace = name.replace(" ", "");
        if (replace.length() != 0) {
            List<ExamMenu> all = examMenuDao.findBySearch(replace);
            PageInfo<ExamMenu> pageInfo = PageInfo.of(all);
            return new ResponseEntity(200,"搜索成功",pageInfo);
        }else {
            List<ExamMenu> all = examMenuDao.clcAdd();
            PageInfo<ExamMenu> pageInfo = PageInfo.of(all);
            return new ResponseEntity(200,"ok",pageInfo);
        }
    }

    @Override
    public ResponseEntity clcAdd() {
        List<ExamMenu> list = examMenuDao.clcAdd();
        return new ResponseEntity(200,"fid成功",list);
    }

    /**
     *@author wen
     *@Discription 查询有fid的题类别
     *@date 2022/4/29 16:37
     **/
    @Override
    public ResponseEntity findCategory() {
        List<ExamMenu> list=examMenuDao.findCategory();
        return new ResponseEntity(200,"fid成功",list);
    }

    @Override
    public ResponseEntity findById(int id) {
        return new ResponseEntity(200,"findById-ok",examMenuDao.findById(id));
    }

    @Override
    public ResponseEntity findFid() {
        ArrayList<String> list = examMenuDao.findFid();
        System.out.println("list:" + list);
        String arr[] = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }
        System.out.println(Arrays.toString(arr));
        return new ResponseEntity(200,"findFid-ok",arr);
    }

    @Override
    public ResponseEntity findAllpaper(int currentPage, int pageSize, int id) {
        PageHelper.startPage(currentPage,pageSize);
        List<ExamPublishexam> all = examMenuDao.findAllpaper(id);
        for (ExamPublishexam ex:all) {
            System.out.println(ex);
            System.out.println(ex.getExamPaperName());
        }
        PageInfo<ExamPublishexam> pageInfo = PageInfo.of(all);
        return new ResponseEntity<>(200,"查询成功",pageInfo);
    }

    @Override
    public ResponseEntity findAllpaperBysearch(int currentPage,int id, String name) {
        PageHelper.startPage(currentPage,6);
        if (name != null) {
            String replace = name.replace(" ", "");
            System.out.println("replace" + replace);
            if(replace != null){
                List<ExamPublishexam> all = examMenuDao.findAllpaper(id);
                for (ExamPublishexam ex:all) {
                    System.out.println(ex);
                }
                List<ExamPublishexam> selectByName = new ArrayList<>();
                for (ExamPublishexam ex:all) {
                    if (ex.getExamPaperName().contains(replace)) {
                        selectByName.add(ex);
                        System.out.println("selectbyname:" + selectByName);
                    }
                }
                PageInfo<ExamPublishexam> pageInfo = PageInfo.of(selectByName);
                return new ResponseEntity<>(200,"查询成功",pageInfo);
            } else {
                return this.findAllpaper(currentPage,6,id);
            }
        }else {
            return this.findAllpaper(currentPage,6,id);
        }
    }

}
